import { request, downloadBlobFile } from 'src/utils';

export interface DeptDto {
  deptId: IdType; // 部门ID
  parentId?: IdType; // 上级ID
  deptName: string; // 部门名称
  sortNo: number; // 排序号
  remark: string; // 备注说明
  isDisabled: boolean; // 是否作废
  children: DeptDto[];
}

const baseUrl = '/api/system/dept';

/**
 * 查单个
 */
export function readData(deptId: IdType): Promise<DeptDto> {
  return request.get(baseUrl + '/' + deptId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<DeptDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 查部门树
 */
export function getDeptTree(query: BaseQuery = {}): Promise<DeptDto[]> {
  return request.get(baseUrl + '/tree', { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(dept: DeptDto): Promise<DeptDto> {
  return dept.deptId ? request.put(baseUrl + '/' + dept.deptId, dept) : request.post(baseUrl, dept);
}
// 更新部门状态
export function updateDeptStatus(deptId: IdType, disabled: boolean): Promise<void> {
  return request.put(baseUrl + '/' + deptId + '/' + disabled);
}
/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<DeptDto>;
